package com.cf.data.serve.controller;

import com.alibaba.druid.util.StringUtils;
import com.cf.data.serve.dto.HkMjDto;
import com.cf.data.serve.dto.PersonDto;
import com.cf.data.serve.service.IssuedService;
import com.cf.data.serve.utils.CommonConstant;
import com.cf.data.serve.utils.RedisUtil;
import com.cf.data.serve.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author cuixi
 * @description: 下发任务接口
 * @date 2024/7/24
 */
@RestController
@RequestMapping("/issued")
@Slf4j
public class IssuedController {
    @Autowired
    private IssuedService issuedService;
    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping(value = "/addFaceDatabase", method = RequestMethod.POST)
    public Result<?> addFaceDatabase(HttpServletRequest req, HttpServletResponse res, @RequestBody PersonDto personDto) {
//        boolean b = authToken(req);
//        if (!b){
//            return Result.error("token不存在或错误！");
//        }
        if (StringUtils.isEmpty(personDto.getNvrId())) {
            return Result.error("nvrId缺失！");
        }
        if (StringUtils.isEmpty(personDto.getImagePath())) {
            return Result.error("imagePath缺失！");
        }
        if (StringUtils.isEmpty(personDto.getPhone())) {
            return Result.error("用户uuid缺失！");
        }
        if (StringUtils.isEmpty(personDto.getName())) {
            return Result.error("用户姓名缺失！");
        }
        if (StringUtils.isEmpty(personDto.getSex())) {
            return Result.error("性别缺失！");
        }
        if (StringUtils.isEmpty(personDto.getLogId())) {
            return Result.error("日志id（logId）缺失！");
        }
        return issuedService.addFacePerson(personDto);
    }


    @RequestMapping(value = "/addMjPerson", method = RequestMethod.POST)
    public Result<?> addMjPerson(HttpServletRequest req, @RequestBody HkMjDto hkMjDto) {
//        boolean b = authToken(req);
//        if (!b){
//            return Result.error("token不存在或错误！");
//        }
        if (StringUtils.isEmpty(hkMjDto.getDevId())) {
            return Result.error("设备id（devId）缺失！");
        }
        if (StringUtils.isEmpty(hkMjDto.getImagePath())) {
            return Result.error("imagePath缺失！");
        }
        if (StringUtils.isEmpty(hkMjDto.getUserId())) {
            return Result.error("用户id缺失！");
        }
        if (StringUtils.isEmpty(hkMjDto.getName())) {
            return Result.error("用户姓名缺失！");
        }
        if (StringUtils.isEmpty(hkMjDto.getLogId())) {
            return Result.error("日志id（logId）缺失！");
        }
        return issuedService.addMjPerson(hkMjDto);
    }

    @RequestMapping(value = "/removePerson", method = RequestMethod.POST)
    public Result<?> removePerson(HttpServletRequest req, String userId) {
//        boolean b = authToken(req);
//        if (!b){
//            return Result.error("token不存在或错误！");
//        }
        if (StringUtils.isEmpty(userId)) {
            return Result.error("userId缺失！");
        }

        return issuedService.removePerson(userId);
    }

    public boolean authToken(HttpServletRequest req) {
        String header = req.getHeader("X-Access-Token");
        if (header == null || header.equals("")) {
            return false;
        }
        return redisUtil.hasKey(CommonConstant.PREFIX_USER_TOKEN + header);
    }
}
